<?php

namespace app\backend\controller;

use app\common\controller\Basic;
use app\common\service\sys\DevLogService;
use think\App;
use think\facade\Db;

/**
 * 开发日志：更新记录
 * @author kaige
 */
class Log extends Basic
{
    //关于OA
    public function index()
    {
        if (request()->isGet()) {
            return _jok('ok', $this->get_sys_info());
        }
        return _jerr(1000);
    }

    /**
     * 获取系统信息
     */
    protected function get_sys_info()
    {
        $sys_info['os'] = PHP_OS;
        $sys_info['web_server'] = $_SERVER['SERVER_SOFTWARE'];
        $sys_info['phpv'] = phpversion();

        $mysqlinfo = Db::query("SELECT VERSION() as version");
        $sys_info['mysql_version']  = $mysqlinfo[0]['version'];
        $sys_info['safe_mode'] = (bool) ini_get('safe_mode') ? 'YES' : 'NO';
        $sys_info['safe_mode_gid'] = (bool) ini_get('safe_mode_gid') ? 'YES' : 'NO';
        $sys_info['socket'] = function_exists('fsockopen') ? 'YES' : 'NO';
        $sys_info['timezone'] = function_exists("date_default_timezone_get") ? date_default_timezone_get() : "no_timezone";
        if (function_exists("gd_info")) {
            $ver_info = gd_info();
            preg_match('/\\d/', $ver_info['GD Version'], $match);
            $gd = $match[0];
            if ($gd == 0) {
                $sys_info['gd'] = '未知';
            } else {
                if ($gd == 1) {
                    $sys_info['gd'] = 'GD1';
                } else {
                    $sys_info['gd'] = 'GD2';
                }
                $sys_info['gd'] .= ' (';
                if ($gd && 0 < (imagetypes() & IMG_JPG)) {
                    $sys_info['gd'] .= ' JPEG';
                }
                if ($gd && 0 < (imagetypes() & IMG_GIF)) {
                    $sys_info['gd'] .= ' GIF';
                }
                if ($gd && 0 < (imagetypes() & IMG_PNG)) {
                    $sys_info['gd'] .= ' PNG';
                }
                $sys_info['gd'] .= ')';
            }
        } else {
            $sys_info['gd'] = "未知";
        }
        $sys_info['zlib']           = function_exists('gzclose') ? 'YES' : 'NO';
        $sys_info['memory_limit']   = ini_get('memory_limit');
        $sys_info['fileupload']     = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknown';

        $sys_info['curl']            = function_exists('curl_init') ? 'YES' : 'NO';
        $sys_info['ip']             = GetHostByName($_SERVER['SERVER_NAME']);
        $sys_info['max_ex_time']     = @ini_get("max_execution_time") . 's'; //脚本最大执行时间
        $sys_info['domain']         = $_SERVER['HTTP_HOST'];
        $sys_info['thinkphp_ver'] = App::VERSION;

        $sys_info['web_run'] = php_sapi_name();
        $sys_info['post_maxsize'] = @ini_get('post_max_size');

        return $sys_info;
    }

    /**
     * 开发规范
     */
    public function sdn()
    {
        if (request()->isGet()) {
            return _jok('ok', []);
        }
        return _jerr(1000);
    }

    /**
     * API说明
     */
    public function apisn()
    {
        if (request()->isGet()) {
            return _jok('ok', []);
        }
        return _jerr(1000);
    }

    /**
     * 开发日志
     */
    public function log()
    {
        if (request()->isGet()) {
            $log = new DevLogService();
            $list = $log->_list([],false);
            return json($list);
        }
        return _jerr(1000);
    }
}
